/**
 * Created by ranliang on 2017.08.17.
 */
var JsBridge = {

    setupWebViewJavascriptBridge: function (callback) {
        var that = this;
        if (window.WebViewJavascriptBridge) {
            return callback(WebViewJavascriptBridge);
        } else {
            document.addEventListener(
                'WebViewJavascriptBridgeReady'
                , function () {
                    callback(WebViewJavascriptBridge)
                },
                false
            );
        }
        if (window.WVJBCallbacks) {
            return window.WVJBCallbacks.push(callback);
        }
        window.WVJBCallbacks = [callback];
        that.WVJBIframe = document.createElement('iframe');
        that.WVJBIframe.style.display = 'none';
        that.WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
        document.documentElement.appendChild(that.WVJBIframe);
        /*setTimeout(function () {
         document.documentElement.removeChild(WVJBIframe)
         }, 0);*/
    },


    //js获取从native传递过来的数据
    transmitDataFromNativeToJS: function (callback, responseData) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.registerHandler('transmitDataFromNativeToJS', function (data, responseCallback) {
            if (data && callback) {
                callback(data);
            }
            responseCallback(responseData);
        })
    }
}